#pragma once
#include <iostream>
#include <string>
#include <cstring>
namespace tom_protocol
{
#define SPACE " "
#define SPACE_LEN strlen(SPACE)

    class Request
    {
    public:
        Request() {}
        Request(int x, int y, char op)
            : _x(x), _y(y), _op(op)
        {
        }
        ~Request() {}
        string Serialize()
        {
            string str;
            str = to_string(_x);
            str += SPACE;
            str += _op;
            str += SPACE;
            str += to_string(_y);
            return str;
        }

        bool Deserialize(const string &str)
        {
            size_t left = str.find(SPACE);
            if (left == string::npos)
                return false;
            size_t right = str.rfind(SPACE);
            if (right == string::npos)
                return false;
            _x = atoi(str.substr(0, left).c_str());
            if ((left + SPACE_LEN) > str.size())
                return false;
            _op = str[left + SPACE_LEN];
            _y = atoi(str.substr(right + SPACE_LEN).c_str());
        }

    public:
        int _x;
        int _y;
        char _op; // + - * /
    };
    class Response
    {
    public:
        Response() {}
        Response(int result, int code)
            : _result(result), _code(code)
        {
        }
        ~Response() {}
        string Serialize()
        {
            string s;
            s = to_string(_code);
            s += SPACE;
            s += to_string(_result);
            return s;
        }
        bool Deserialize(const string &s)
        {
            size_t pos = s.find(SPACE);
            if (pos == string::npos)
                return false;
            _code = atoi(s.substr(0, pos).c_str());
            _result = atoi(s.substr(pos + SPACE_LEN).c_str());
            return true;
        }

    public:
        int _result;
        int _code;
    };

    string Recv(int sock)
    {
        char buffer[1024];
        ssize_t s = recv(sock, buffer, sizeof(buffer), 0);
        if (s > 0)
            return buffer;
    }
    void Send(int sock, const string &str)
    {
        send(sock, str.c_str(), str.size(), 0);
    }
} // namespace tom_protocol
